/**
 * @NApiVersion 2.x
 * @NScriptType MapReduceScript
 */
define(
    ['N/search', 'N/record', 'N/task', 'N/runtime',
        'N/file'
    ],
    function (search, record, task, runtime, file) {
        function getInputData() {
            var searchid = runtime.getCurrentScript().getParameter('custscript_searchid');
            // log.debug({
            // 	title: 'searchid',
            // 	details: JSON.stringify(searchid)
            // });
            var mySearch = search.load(searchid);
            var results = mySearch.run().getRange({
                start: 0,
                end: 1000
            });
            // log.debug({
            //     title: 'results',
            //     details: JSON.stringify(results)
            // });
            var alls = [];
            var j = 1;
            while (results.length > 0 && j < 100) {
                for (var i = 0; i < results.length; i++) {
                    var result = results[i];
                    var tmp = new Object();
                    tmp.id = result.id;
                    tmp.type = result.getValue('type');
                    alls[alls.length] = tmp;
                }
                results = mySearch.run().getRange({
                    start: 0 + j * 1000,
                    end: 1000 + j * 1000
                });
                j++;
            }
            log.debug({
                title: 'alls',
                details: JSON.stringify(alls.length)
            });
            return alls;
        }

        function map(context) {
            // log.debug({
            // 	title: 'context',
            // 	details: JSON.stringify(context)
            // });
            try {
                var value = JSON.parse(context.value);
                var searchid = runtime.getCurrentScript().getParameter('custscript_searchid');
                var mySearch = search.load(searchid);
                // log.debug({
                //     title: 'value.type',
                //     details: JSON.stringify(value.type)
                // });
                var type = '';
                if (value.type == 'SCHEDULED') {
                    type = record.Type.SCHEDULED_SCRIPT;
                } else if (value.type == 'MAPREDUCE') {
                    type = record.Type.MAP_REDUCE_SCRIPT;
                } else if (value.type == 'InvAdjst') {
                    type = record.Type.INVENTORY_ADJUSTMENT;
                } else if (value.type == 'SalesOrd') {
                    type = record.Type.SALES_ORDER;
                } else if (value.type == 'PurchOrd') {
                    type = record.Type.PURCHASE_ORDER;
                } else if (value.type == 'ItemRcpt') {
                    type = record.Type.ITEM_RECEIPT;
                } else if (value.type == 'VendBill') {
                    type = record.Type.VENDOR_BILL;
                } else if (value.type == 'CashSale') {
                    type = record.Type.CASH_SALE;
                } else if (value.type == 'Opprtnty') {
                    type = record.Type.OPPORTUNITY;
                } else if (value.type == 'VendPymt') {
                    type = record.Type.VENDOR_PAYMENT;
                } else if (value.type == 'RtnAuth') {
                    type = record.Type.RETURN_AUTHORIZATION;
                } else if (value.type == 'BinTrnfr') {
                    type = record.Type.BIN_TRANSFER;
                } else if (value.type == 'TrnfrOrd') {
                    type = record.Type.TRANSFER_ORDER;
                } else if (value.type == 'ItemShip') {
                    type = record.Type.ITEM_FULFILLMENT;
                } else if (value.type == 'VendAuth') {
                    type = record.Type.VENDOR_RETURN_AUTHORIZATION;
                } else if (value.type == 'VendCred') {
                    type = record.Type.VENDOR_CREDIT;
                } else if (value.type == 'BinWksht') {
                    type = record.Type.BIN_WORKSHEET;
                } else if (value.type == 'WorkOrd') {
                    type = record.Type.WORK_ORDER;
                } else if (value.type == 'WOCompl') {
                    type = record.Type.WORK_ORDER_COMPLETION;
                } else if (value.type == 'WOIssue') {
                    type = record.Type.WORK_ORDER_ISSUE;
                } else if (value.type == 'WOClose') {
                    type = record.Type.WORK_ORDER_CLOSE;
                } else if (value.type == 'Estimate') {
                    type = record.Type.ESTIMATE;
                } else if (value.type == 'InvCount') {
                    type = record.Type.INVENTORY_COUNT;
                } else if (value.type == 'PurchReq') {
                    type = record.Type.PURCHASE_REQUISITION;
                } else if (value.type == 'Journal') {
                    type = record.Type.JOURNAL_ENTRY;
                } else if (value.type == 'Deposit') {
                    type = record.Type.DEPOSIT;
                } else if (value.type == 'ExpRept') {
                    type = record.Type.EXPENSE_REPORT;
                } else if (value.type == 'CustDep') {
                    type = record.Type.CUSTOMER_DEPOSIT;
                } else if (value.type == 'Build') {
                    type = record.Type.ASSEMBLY_BUILD;
                } else if (value.type == 'Rfq') {
                    type = record.Type.ASSEMBLY_BUILD;
                } else if (value.type == 'InvtPart') {
                    type = record.Type.INVENTORY_ITEM;
                } else if (value.type == 'Kit') {
                    type = record.Type.KIT_ITEM;
                } else if (value.type == 'Description') {
                    type = record.Type.DESCRIPTION_ITEM;
                } else if (value.type == 'Discount') {
                    type = record.Type.DISCOUNT_ITEM;
                } else if (value.type == 'Service') {
                    type = record.Type.SERVICE_ITEM;
                } else if (value.type == 'Assembly') {
                    type = record.Type.ASSEMBLY_ITEM;
                } else if (value.type == 'NonInvtPart') {
                    type = record.Type.NON_INVENTORY_ITEM;
                } else if (value.type == 'OthCharge') {
                    type = record.Type.OTHER_CHARGE_ITEM;
                } else if (value.type == 'Payment') {
                    type = record.Type.PAYMENT_ITEM;
                }else if (value.type == 'InvTrnfr') {
					type = record.Type.INVENTORY_TRANSFER;
				}
				else if (value.type == 'CustCred') {
					type = record.Type.CREDIT_MEMO;
				}
				else if (value.type == 'RtnAuth') {
					type = record.Type.RETURN_AUTHORIZATION;
				}
				else if (value.type == 'CustRfnd') {
					type = record.Type.CUSTOMER_REFUND;
				}
				else if (value.type == 'RtnAuth') {
					type = record.Type.RETURN_AUTHORIZATION;
				}
                else {
                    type = mySearch.searchType;
                }

               var deleteId =  record.delete({
                    type: type,
                    id: Number(value.id)
                });
                log.debug({
                    title: type+'   deleteId',
                    details: deleteId
                });

            } catch (e) {
                log.error({
                    title: 'netsuite error',
                    details: e.message + ',' + e.stack
                });
            }
        }


        function reduce(context) {
            // log.debug({
            // 	title: 'context',
            // 	details: context
            // });
            // try {
            // 	var value = JSON.parse(context.values[0]);
            //
            // } catch (e) {
            // 	log.error({
            // 		title: 'netsuite error',
            // 		details: e.message + ',' + e.stack
            // 	});
            // }
        }


        function summarize(summary) {
            // var mrTask1 = task.create({
            // 	taskType: task.TaskType.SCHEDULED_SCRIPT,
            // 	scriptId: 90,
            // 	deploymentId: 'customdeploy_associator2',
            // });
            // var mrTaskId1 = mrTask1.submit();
            // log.debug({
            // 	title: 'mrTaskId1',
            // 	details: JSON.stringify(mrTaskId1)
            // });
            // log.debug({
            // 	title: 'summary',
            // 	details: summary
            // });
        }

        return {
            getInputData: getInputData,
            map: map,
            reduce: reduce,
            summarize: summarize
        };
    });